package com.rules.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.gillion.grule.client.core.DataSourceClient;
import com.gillion.grule.client.core.SqlConfigClient;
import com.google.common.collect.Lists;
import com.rules.admin.entity.SqlConfigModel;
import com.rules.admin.dao.DataSourceModelDao;
import com.rules.admin.entity.DataSourceModel;
import com.rules.admin.service.DataSourceModelService;
import com.rules.admin.service.SqlConfigModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class DataSourceModelServiceImpl extends ServiceImpl<DataSourceModelDao , DataSourceModel> implements DataSourceModelService {
   @Autowired
   private SqlConfigModelService sqlConfigModelService;

   public List<DataSourceModel> selectListByProjectId(String projectId) {

      LambdaQueryWrapper<DataSourceModel> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(DataSourceModel::getProjectId, projectId);
      return this.baseMapper.selectList(queryWrapper);

   }

   public Map<String, DataSourceModel> selectMapByProjectId(String projectId) {
      return null;
   }

   public List<DataSourceClient> selectMapByProjectCode(String projectCode) {
      List<DataSourceClient> list = Lists.newArrayList();
      LambdaQueryWrapper<DataSourceModel> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(DataSourceModel::getProjectCode, projectCode);
      List<DataSourceModel> dataSourceModelsList = this.baseMapper.selectList(queryWrapper);
      Iterator var5 = dataSourceModelsList.iterator();

      while(var5.hasNext()) {
         DataSourceModel dataSourceModel = (DataSourceModel)var5.next();
         DataSourceClient dataSourceClient = new DataSourceClient();
         List<SqlConfigClient> sqlConfigClients = Lists.newArrayList();
         dataSourceClient.setDatasourceName(dataSourceModel.getDatasourceName());
         dataSourceClient.setUrl(dataSourceModel.getUrl());
         dataSourceClient.setPassword(dataSourceModel.getPassword());
         dataSourceClient.setUsername(dataSourceModel.getUsername());
         dataSourceClient.setSqlConfigClient(sqlConfigClients);
         List<SqlConfigModel> sqlList = this.sqlConfigModelService.selectListByDataSourceId(dataSourceModel.getId().toString());
         Iterator var10 = sqlList.iterator();

         while(var10.hasNext()) {
            SqlConfigModel sqlConfigModel = (SqlConfigModel)var10.next();
            SqlConfigClient sqlConfigClient = new SqlConfigClient();
            sqlConfigClient.setSqlName(sqlConfigModel.getSqlName());
            sqlConfigClient.setSqlExpress(sqlConfigModel.getSqlExpress());
            sqlConfigClient.setSqlParam(sqlConfigModel.getSqlParam());
            sqlConfigClient.setSqlResult(sqlConfigModel.getSqlResult());
            sqlConfigClients.add(sqlConfigClient);
         }

         list.add(dataSourceClient);
      }

      return list;
   }

}
